package com.cnzhy.controller;

/**
 * @Author: zhaoyan5
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;
import java.util.stream.IntStream;

/**
 * @author: elvin
 */
@RestController
@RequestMapping("/publish")
public class PublishController {

    private JmsMessagingTemplate jms;

    private Queue queue;

    private Topic topic;

    private final static int NUM = 10;

    @Autowired
    public void setJms(JmsMessagingTemplate jms) {
        this.jms = jms;
    }

    @Autowired
    public void setQueue(Queue queue) {
        this.queue = queue;
    }

    @Autowired
    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    @RequestMapping("/queue")
    public String queue() {
        IntStream.range(0, 10).forEach(i -> jms.convertAndSend(queue, "queue" + i));
        return "queue 发送成功";
    }

    @JmsListener(destination = "out.queue")
    public void consumerMsg(String msg) {
        System.out.println(msg);
    }

    @RequestMapping("/topic")
    public String topic() {
        IntStream.range(0, 10).forEach(i -> jms.convertAndSend(topic, "topic" + i));
        return "topic 发送成功";
    }
}
